package com.songlanyun.modules.studyingType.controller.admin;

import cn.hutool.core.util.ObjectUtil;
import com.songlanyun.common.exception.RRException;
import com.songlanyun.common.utils.R;
import com.songlanyun.common.validator.ValidatorUtils;
import com.songlanyun.modules.studyingType.entity.StudyingType;
import com.songlanyun.modules.studyingType.service.StudyingTypeService;
import com.songlanyun.modules.user.controller.AbstractController;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;


@RestController
@RequestMapping("/admin/studyingtype")
public class StudyingTypeController extends AbstractController {

    @Autowired
    private StudyingTypeService studyingTypeService;


    //下拉框，树状结构
    @GetMapping("/getAllStudy")
    public R getAllStudy() {
        List<StudyingType> studyingTypes = studyingTypeService.getAllStudy();
        return R.ok().put("data", studyingTypes);
    }

    @GetMapping("/{id}")
    public R info(@PathVariable("id") Long id) {
        StudyingType studyingType = studyingTypeService.loadById(id, true);
        return R.ok().put("data", studyingType);
    }

    /**
     * 新增
     */
    @PostMapping("/add")
    public R add(@RequestBody StudyingType studyingType) {
        ValidatorUtils.validateEntity(studyingType);
        studyingType.setCreateBy(getUserId());
        studyingType.setUpdateBy(getUserId());
        StudyingType resstudying = studyingTypeService.add(studyingType);
        return R.ok().put("data", resstudying);
    }

    /**
     * 修改
     */
    @PutMapping("/update")
    public R update(@RequestBody StudyingType studyingType) {
        if (ObjectUtil.isNull(studyingType.getId())) {
            throw new RRException("ID不能为空");
        }
        ValidatorUtils.validateEntity(studyingType);
        studyingType.setUpdateBy(getUserId());
        StudyingType resstudying = studyingTypeService.update(studyingType);
        return R.ok().put("data", resstudying);
    }

    /**
     * 删除
     */
    @DeleteMapping("/delete/{id}")
    public R delete(@PathVariable("id") Integer id) {
        studyingTypeService.delete(id);
        return R.ok();
    }
}
